{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ffe3dd8c-d666-45bb-8b3d-2468330d1038",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=var('x')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7c8cc1dd-c7a1-4f2d-8a3c-13a11d964bf3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle {\\left(x^{x} {\\left(\\log\\left(x\\right) + 1\\right)} \\log\\left(x\\right) + \\frac{x^{x}}{x}\\right)} x^{\\left(x^{x}\\right)}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle {\\left(x^{x} {\\left(\\log\\left(x\\right) + 1\\right)} \\log\\left(x\\right) + \\frac{x^{x}}{x}\\right)} x^{\\left(x^{x}\\right)}$"
      ],
      "text/plain": [
       "(x^x*(log(x) + 1)*log(x) + x^x/x)*x^(x^x)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "show(diff(x^(x^x),x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fe1c2554-b65e-4a3a-bc3d-0d7dd97d7523",
   "metadata": {},
   "outputs": [],
   "source": [
    "P(n)=function(\"P\")(x^P(n-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ac067579-b394-41f2-9e91-c52fd477d1cc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "72a562ee-0a86-42b7-93fc-390a357ecfae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "P(x^P(2))"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "13a9eef9-b1be-4840-8709-51414472fbac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[P(x^P(2)) == r1]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solve(P(1)==x,P(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "68390f78-23d2-47ac-ad22-323f378b8ade",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sqrt(5)*(-(1 - sqrt(5))**n + (1 + sqrt(5))**n)/(5*2**n)\n"
     ]
    }
   ],
   "source": [
    "from sympy import Function, rsolve\n",
    "from sympy.abc import n\n",
    "a = Function('a')\n",
    "\n",
    "# 定义递推方程：a(n) = a(n-1) + a(n-2)\n",
    "eq = a(n) - a(n-1) - a(n-2)\n",
    "\n",
    "# 初始条件：a(0)=0, a(1)=1\n",
    "ics = {a(0): 0, a(1): 1}\n",
    "\n",
    "# 求解通项\n",
    "solution = rsolve(eq, a(n), ics)\n",
    "print(solution.simplify())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "94363211-22f2-4d18-97e8-7e5aa380fc76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x*((-1)**n + 1)/2\n"
     ]
    }
   ],
   "source": [
    "from sympy import Function, rsolve\n",
    "from sympy.abc import n\n",
    "a = Function('a')\n",
    "\n",
    "# 定义递推方程：a(n) = a(n-1) + a(n-2)\n",
    "eq = a(n) - x+a(n-1)\n",
    "\n",
    "# 初始条件：a(0)=0, a(1)=1\n",
    "ics = {a(0): x}\n",
    "\n",
    "# 求解通项\n",
    "solution = rsolve(eq, a(n), ics)\n",
    "print(solution.simplify())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 10.5",
   "language": "sage",
   "name": "sagemath"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
